// Copyright (c) 2005-2008, Simon Howard
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use std::cmp::Ordering;
use std::collections::BinaryHeap;

const NUM_TEST_VALUES: usize = 10000;

#[derive(Debug, PartialEq, Eq)]
enum HeapType {
    Min,
    Max,
}

#[derive(Debug)]
struct BinomialHeap<T: Ord> {
    heap: BinaryHeap<T>,
    heap_type: HeapType,
}

impl<T: Ord> BinomialHeap<T> {
    /// Creates a new binomial heap with the specified type (min or max).
    pub fn new(heap_type: HeapType) -> Self {
        let heap = match heap_type {
            HeapType::Min => BinaryHeap::new(),
            HeapType::Max => BinaryHeap::new(),
        };
        BinomialHeap { heap, heap_type }
    }

    /// Inserts a value into the heap.
    pub fn insert(&mut self, value: T) {
        self.heap.push(value);
    }

    /// Pops the top value from the heap.
    pub fn pop(&mut self) -> Option<T> {
        self.heap.pop()
    }

    /// Returns the number of entries in the heap.
    pub fn num_entries(&self) -> usize {
        self.heap.len()
    }
}

#[test]
fn test_binomial_heap_new_free() {
    for _ in 0..NUM_TEST_VALUES {
        let mut heap = BinomialHeap::new(HeapType::Min);
        drop(heap); // Explicitly drop the heap to simulate free
    }

    // Test for out of memory (not applicable in Rust's safe memory model)
}

#[test]
fn test_binomial_heap_insert() {
    let mut heap = BinomialHeap::new(HeapType::Min);
    let mut test_array = [0; NUM_TEST_VALUES];

    for i in 0..NUM_TEST_VALUES {
        test_array[i] = i as i32;
        heap.insert(test_array[i]);
    }
    assert_eq!(heap.num_entries(), NUM_TEST_VALUES);

    // Test for out of memory (not applicable in Rust's safe memory model)
}

#[test]
fn test_min_heap() {
    let mut heap = BinomialHeap::new(HeapType::Min);
    let mut test_array = [0; NUM_TEST_VALUES];

    for i in 0..NUM_TEST_VALUES {
        test_array[i] = i as i32;
        heap.insert(test_array[i]);
    }

    let mut i = -1;
    while heap.num_entries() > 0 {
        let val = heap.pop().unwrap();
        assert_eq!(val, i + 1);
        i = val;
    }

    // Test pop on an empty heap
    assert_eq!(heap.pop(), None);
}

#[test]
fn test_max_heap() {
    let mut heap = BinomialHeap::new(HeapType::Max);
    let mut test_array = [0; NUM_TEST_VALUES];

    for i in 0..NUM_TEST_VALUES {
        test_array[i] = i as i32;
        heap.insert(test_array[i]);
    }

    let mut i = NUM_TEST_VALUES as i32;
    while heap.num_entries() > 0 {
        let val = heap.pop().unwrap();
        assert_eq!(val, i - 1);
        i = val;
    }

    // Test pop on an empty heap
    assert_eq!(heap.pop(), None);
}

#[test]
fn test_insert_out_of_memory() {
    // Rust's memory safety model does not allow for out-of-memory scenarios in the same way as C.
    // This test is not applicable in Rust's safe memory model.
}

#[test]
fn test_pop_out_of_memory() {
    // Rust's memory safety model does not allow for out-of-memory scenarios in the same way as C.
    // This test is not applicable in Rust's safe memory model.
}
